/*
 * Copyright 2010 Groupware.cc All right reserved. This software is the
 * confidential and proprietary information of Groupware.cc ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Groupware.cc.
 */
package com.rome.syncml.bean.interceptor;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.rome.syncml.domain.User;

/**
 * 类UserInterceptor.java的实现描述：TODO 类实现描述 
 * @author afei 2011-11-26 下午07:50:47
 */
public class UserInterceptor extends HandlerInterceptorAdapter{

    
    private String redirectUrl;
    
    private List<String> authenticateUrls;
    
    /* (non-Javadoc)
     * @see org.springframework.web.servlet.handler.HandlerInterceptorAdapter#preHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println(request.getServletPath());
        String path = request.getServletPath();
        if(authenticateUrls.contains(path)){
            User user = (User)request.getSession().getAttribute("user");
            if(user==null || user.getId()==null || user.getId()==0){
                response.sendRedirect(redirectUrl);
                return false;
            }
        }
        return true;
    }
    
    
    /**
     * @param redirectUrl the redirectUrl to set
     */
    public void setRedirectUrl(String redirectUrl) {
        this.redirectUrl = redirectUrl;
    }
    
    
    /**
     * @return the redirectUrl
     */
    public String getRedirectUrl() {
        return redirectUrl;
    }
    
    
    /**
     * @param authenticateUrls the authenticateUrls to set
     */
    public void setAuthenticateUrls(List<String> authenticateUrls) {
        this.authenticateUrls = authenticateUrls;
    }
}
